package blue.datacenter;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class Message {
	private String sip_;  // Source IP, where is Message from.
	private String port_;  // ID for slave.
	private String data_; //
	
	public Message(String json_str){
		// Input String : sip:{1.1.1.1},port:{1},data:{xxx}
		//System.out.println("Messge------"+json_str);
		String pattern_str = "sip:\\{(.*)\\},port:\\{(.*)\\},data:\\{(.*)\\}";
		Pattern  regex = Pattern.compile(pattern_str,Pattern.DOTALL);
		Matcher mat = regex.matcher(json_str);
		if (mat.find()) {
			sip_ = mat.group(1);
			port_ = mat.group(2);
			data_ = mat.group(3);
		}else{
			System.out.println("Error: Failed to parse msg. <"+json_str+">");
		}
	}
	public Message(String sip, String data){
		sip_ = sip;
		data_ = data;
	}
	
	public Message(String sip, String slave_id, String data){
		sip_ = sip;
		port_ = slave_id;
		data_ = data;
	}
	
	public String getSip(){return sip_;}
	public String getPort(){return port_;}
	public String getData(){return data_;}
	
	/*public boolean fromJSON(String json_str){
		// TODO set obj from JSON.
		return true;
	}*/
	public String toJSON(){
		// TODO transform obj to JSON.
		String json_str = "sip:{"+sip_+"},port:{"+port_+"},data:{"+data_+"}";
		return json_str;
	}
}
